<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

function otx_router_parse($route)
{
	if (!empty($route)) {
		if (false !== strpos($route, '/')) {
			list($module_name, $controller_name) = explode('/', $route, 2);
		} else {
			$module_name = $route;
			$controller_name = 'index';
		}
	} else {
		$module_name = 'index';
		$controller_name = 'index';
	}

	$action_name = otx_param_string('action');
	if (empty($action_name)) {
		$action_name = 'index';
	}

	otx_router_module($module_name);
	otx_router_controller($controller_name);
	otx_router_action($action_name);
	otx_param_set('route', $module_name . '/' . $controller_name);
}

function otx_router_execute($module_name = null, $controller_name = null, $action_name = null)
{
	if (null === $module_name) {
		$module_name = otx_router_module();
	}

	if (null === $controller_name) {
		$controller_name = otx_router_controller();
	}

	if (null === $action_name) {
		$action_name = otx_router_action();
	}

	$obLevel = ob_get_level();
	ob_start();

	try {
		$controller_file = otx_module_path($module_name,
				'controllers/' . $controller_name . '.php');
		if (!file_exists($controller_file)) {
			throw new Exception('Controller file not exists: ' . $controller_file);
		}

		include_once $controller_file;

		$class_name = $module_name . '_controller_' . $controller_name;
		if (!class_exists($class_name)) {
			throw new Exception('Controller class not exists: ' . $class_name);
		}

		$instance = new $class_name();
		$instance->module_name = $module_name;
		$instance->controller_name = $controller_name;
		$instance->action_name = $action_name;

		$method_name = $action_name . '_action';
		if (!method_exists($instance, $method_name)) {
			throw new Exception('Controller method not exists: ' . $method_name);
		}

		otx_param_set('_controller_file', $controller_file);
		otx_language_load('module', $module_name, $module_name);
		otx_language_load('module', $module_name, $controller_name);

		otx_event_dispatch('router_dispatch_before');
		$instance->$method_name();
		otx_event_dispatch('router_dispatch_after');
	} catch (Exception $e) {
		$curObLevel = ob_get_level();
		if ($curObLevel > $obLevel) {
			do {
				ob_get_clean();
				$curObLevel = ob_get_level();
			} while ($curObLevel > $obLevel);
		}

		throw $e;
	}

	$content = ob_get_clean();
	otx_param_set('_content', $content);
}

function otx_router_module($module_name = null)
{
	if (null === $module_name) {
		return otx_param_string('_module_name');
	}
	otx_param_set('_module_name', $module_name);
}

function otx_router_controller($controller_name = null)
{
	if (null === $controller_name) {
		return otx_param_string('_controller_name');
	}
	otx_param_set('_controller_name', $controller_name);
}

function otx_router_action($action_name = null)
{
	if (null === $action_name) {
		return otx_param_string('_action_name');
	}
	otx_param_set('_action_name', $action_name);
}

function otx_router_url($action_name, $controller_name = null, $module_name = null, $params = array())
{
	if (null === $controller_name) {
		$controller_name = otx_router_controller();
	}

	if (null === $module_name) {
		$module_name = otx_router_module();
	}

	$url = otx_request_base_url()
		. '?route=' . $module_name . '/' . $controller_name
		. '&action=' . $action_name;

	foreach ((array) $params as $key => $value) {
		$url .= '&' . urlencode($key) . '=' . urlencode($value);
	}

	return $url;
}

function otx_router_redirect($action_name, $controller_name = null, $module_name = null, $params = array())
{
	header('Location: ' . otx_router_url($action_name, $controller_name, $module_name, $params));
	exit();
}